﻿using System;
using System.Collections.Generic;
using System.IO;

namespace AdaptiveAgents.Loggers
{
    class GraphLogger : Logger
    {
        private String fileName;
        private StreamWriter fout;

        public GraphLogger(String name)
        {
            fileName = name;

            try
            {
                // Open a file stream writer
                fout = new StreamWriter(fileName);
            }
            // Catches any error conditions
            catch (IOException e)
            {
                System.Console.Error.WriteLine("Unable to write to graph file: " + fileName);
                System.Diagnostics.Debug.Assert(false);
            }
        }

        /*public override void Write(MessageType message, String data)
        {
            if (message == MessageType.EpsilonGraph)
               fout.Write(data + ",");
        }*/

        public override void Close()
        {
            try
            {
                fout.Close();
            }
            catch (IOException e)
            {
                System.Console.Error.WriteLine("Problem closing file: " + fileName);
            }
        }

        protected override void writeInternal(MessageType message, bool newLine, params object[] p)
        {
            bool isWrite = true;

            switch (message)
            {
                case MessageType.EpsilonGraph:
                    foreach (object obj in p)
                    {
                        fout.Write(p);
                        fout.Write(",");
                    }
                    break;
                default:
                    isWrite = false;
                    break;
            }

            if (newLine && isWrite)
                fout.WriteLine();
        }
    }
}
